package com.eeboot.util;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {

    public static DateTimeFormatter getDefaultFormatter(){
        return getDateTimeFormatter("yyyy-MM-dd HH:mm:ss");
    }

    public static DateTimeFormatter getDateTimeFormatter(String pattern){
        return DateTimeFormatter.ofPattern(pattern);
    }

    public static String getNow(DateTimeFormatter formatter){
        LocalDateTime date =  LocalDateTime.now();
        return date.format(formatter);
    }

    //获取指定时间的指定格式
    public static String formatTime(LocalDateTime time,String pattern) {
        return time.format(getDateTimeFormatter(pattern));
    }

    //Date转换为LocalDateTime
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime转换为Date
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    //字符串转LocalDateTime
    public static LocalDateTime getDateFromStr(DateTimeFormatter formatter, String date){
        return LocalDateTime.parse(date, formatter);
    }

    //获取long
    public static Long getLong(DateTimeFormatter formatter, String date){
        return getSecondsByTime(getDateFromStr(formatter, date));
    }

    public static Long getLong(String formatter, String date){
        return getLong(getDateTimeFormatter(formatter), date);
    }

    public static String getStringFromLong(String pattern, Long date){
        return formatTime(convertDateToLDT(new Date(date)), pattern);
    }

    public static void main(String[] args) {
        System.out.println(DateUtil.getLong("yyyy-MM-dd HH:mm:ss.SSS","2019-04-24 10:49:55.594"));
    }

}
